package com.eling.elcms.travel.dao.hibernate;

import com.eling.elcms.core.AppContext;
import com.eling.elcms.core.dao.hibernate.GenericDaoHibernate;
import com.eling.elcms.system.model.CommonUser;
import com.eling.elcms.travel.dao.IPlanDao;
import com.eling.elcms.travel.model.Destination;
import com.eling.elcms.travel.model.Plan;
import com.eling.elcms.travel.model.PlanItem;
import com.eling.elcms.travel.model.view.PlanItemView;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Repository
public class PlanDaoHibernate extends GenericDaoHibernate<Plan, Long> implements IPlanDao{

	public PlanDaoHibernate() {
		super(Plan.class);
		// TODO Auto-generated constructor stub
	}

	@SuppressWarnings("unchecked")
	@Override
	public PlanItemView seletplan(Plan plan) {
		Calendar c = Calendar.getInstance();
		c.setTime(plan.getYear());
		CommonUser cu=(CommonUser)AppContext.curUser();
		StringBuffer sql = new StringBuffer();
		sql.append("	SELECT 										");
		sql.append("	    p.verNumber,p.planTitle,p.editor,p.editorDate,p.introduction,p.explanation,p.pkPlan,	");
		sql.append("	    pi.startDate,pi.endDate,pi.limitmax,pi.maxpeople,pi.limitmin,pi.minPeople,des.pkDestination,des.`name`	");
		sql.append("	FROM										");
		sql.append("		travel_plan p							");
		sql.append("	LEFT JOIN travel_plan_planitem pp ON p.pkPlan = pp.pkPlan		");
		sql.append("	LEFT JOIN travel_planitem pi ON pp.pkPlanItem = pi.pkPlanItem	");
		sql.append("	LEFT JOIN travel_destination des ON des.pkDestination = pi.pkDestination	");
		sql.append("	WHERE															");
		sql.append("		p.verNumber='"+plan.getVerNumber()+"'							");
		sql.append("	AND p.`year` LIKE '"+c.get(Calendar.YEAR)+"%'					");
		sql.append("	AND p.pkOrganization="+cu.getOrganization().getPkOrganization()+"	");
		Query sqlQuery = getSession().createNativeQuery(sql.toString());
		List<PlanItem> list=new ArrayList<PlanItem>();
		List<Object[]> lists = sqlQuery.list();
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		PlanItemView pv=new PlanItemView();
		Plan p =new Plan();
		for(Object[] o:lists){
			p.setVerNumber(o[0] ==null ? null : o[0].toString());
			p.setYear(plan.getYear());
			p.setPlanTitle(o[1] ==null ? null : o[1].toString());
			p.setEditor(o[2] ==null ? null : o[2].toString());
			p.setIntroduction(o[4] ==null ? null : o[4].toString());
			p.setExplanation(o[5] ==null ? null : o[5].toString());
			p.setPkPlan(o[6] ==null ? null : Long.valueOf(o[6].toString()));
			PlanItem pi=new PlanItem();
			if(o[9].toString().equals("true")){
				pi.setLimitmax(true);
			}else{
				pi.setLimitmax(false);
			}
			pi.setMaxpeople(o[10] ==null ? null : Integer.valueOf(o[10].toString()));
			if(o[11].toString().equals("true")){
				pi.setLimitmin(true);
			}else{
				pi.setLimitmin(false);
			}
			pi.setMinpeople(o[12] ==null ? null : Integer.valueOf(o[12].toString()));
			Destination d=new Destination();
			d.setPkDestination(o[13] ==null ? null : Long.valueOf(o[13].toString()));
			d.setName(o[14] ==null ? null : o[14].toString());
			pi.setDestination(d);
			try {
				p.setEditorDate(o[3] ==null ? null : sdf.parse(o[3].toString()));
				pi.setStartDate(o[7] ==null ? null : sdf.parse(o[7].toString()));
				pi.setEndDate(o[8] ==null ? null : sdf.parse(o[8].toString()));
			} catch (ParseException e) {
					e.printStackTrace();
			}
			list.add(pi);
		}
		pv.setList(list);
		pv.setPlan(p);
		return pv;
	}
}
